#include "mysqldb.h"
#include <QDebug>

MysqlDB::MysqlDB(const QString &connectionName, QObject *parent, const DatabaseInfo &dbInfo)
    : QObject(parent)
    , m_dbInfo(dbInfo)
    , m_connName(connectionName)
{
}

MysqlDB::~MysqlDB()
{
}

void MysqlDB::setConntionName(const QString &name)
{
    m_connName = name;
}

void MysqlDB::setDatabaseInfo(const DatabaseInfo &info)
{
    setHostName(info.server);
    setUserName(info.user);
    setPassword(info.passwd);
    setPort(info.port);
    setDatabaseName(info.dbName);
}

void MysqlDB::setHostName(const QString &host)
{
    m_dbInfo.server = host;
}

void MysqlDB::setUserName(const QString &user)
{
    m_dbInfo.user = user;
}

void MysqlDB::setPassword(const QString &pwd)
{
    m_dbInfo.passwd = pwd;
}

void MysqlDB::setDatabaseName(const QString &dbName)
{
    m_dbInfo.dbName = dbName;
}

void MysqlDB::setPort(int port)
{
    m_dbInfo.port = port;
}

bool MysqlDB::testConnect()
{
    QString name;
    bool ok;
    try
    {
        QSqlDatabase db;
        if(m_connName.isEmpty())
            db = QSqlDatabase::addDatabase("QMYSQL");
        else
            db = QSqlDatabase::addDatabase("QMYSQL", m_connName);
        db.setHostName(m_dbInfo.server);
        db.setPort(m_dbInfo.port);
        db.setUserName(m_dbInfo.user);
        db.setPassword(m_dbInfo.passwd);
        name = db.connectionName();
        ok = db.open();
        if(!ok)
        {
            qInfo() << db.lastError().text();
        }
        db.close();
    }
    catch(const QSqlError &e)
    {
        qInfo() << e.text();
    }

    QSqlDatabase::removeDatabase(name);
    return ok;
}

QStringList MysqlDB::databaseNames()
{
    QStringList data;
    QString name;
    bool ok;
    try
    {
        QSqlDatabase db;
        if(m_connName.isEmpty())
            db = QSqlDatabase::addDatabase("QMYSQL");
        else
            db = QSqlDatabase::addDatabase("QMYSQL", m_connName);
        db.setHostName(m_dbInfo.server);
        db.setPort(m_dbInfo.port);
        db.setUserName(m_dbInfo.user);
        db.setPassword(m_dbInfo.passwd);
        name = db.connectionName();
        ok = db.open();
        if(!ok)
        {
            qInfo() << "open database failed, " << db.lastError().text();
        }

        if(!db.isOpen())
            return data;
        QSqlQuery query(db);
        query.exec("SHOW DATABASES;");
        while(query.next())
        {
            data.push_back(query.value(0).toString());
        }
        db.close();
    }
    catch(const QSqlError &e)
    {
        qInfo() << e.text();
    }

    QSqlDatabase::removeDatabase(name);
    return data;
}

QStringList MysqlDB::tables()
{
    QStringList data;
    QString name;
    bool ok;
    try
    {
        QSqlDatabase db;
        if(m_connName.isEmpty())
            db = QSqlDatabase::addDatabase("QMYSQL");
        else
            db = QSqlDatabase::addDatabase("QMYSQL", m_connName);
        db.setHostName(m_dbInfo.server);
        db.setPort(m_dbInfo.port);
        db.setUserName(m_dbInfo.user);
        db.setPassword(m_dbInfo.passwd);
        db.setDatabaseName(m_dbInfo.dbName);
        name = db.connectionName();
        ok = db.open();
        if(!ok)
        {
            qInfo() << "open database failed, " << db.lastError().text();
        }

        if(!db.isOpen())
            return data;
        QSqlQuery query(db);
        query.exec("SHOW TABLES;");
        while(query.next())
        {
            data.push_back(query.value(0).toString());
        }
        db.close();
    }
    catch(const QSqlError &e)
    {
        qInfo() << e.text();
    }

    QSqlDatabase::removeDatabase(name);
    return data;
}

void MysqlDB::sqlData(const QString &sql, QSqlRecord &record, QVector<QVariant> &data)
{
    QString name;
    bool ok;
    try
    {
        QSqlDatabase db;
        if(m_connName.isEmpty())
            db = QSqlDatabase::addDatabase("QMYSQL");
        else
            db = QSqlDatabase::addDatabase("QMYSQL", m_connName);
        db.setHostName(m_dbInfo.server);
        db.setPort(m_dbInfo.port);
        db.setUserName(m_dbInfo.user);
        db.setPassword(m_dbInfo.passwd);
        db.setDatabaseName(m_dbInfo.dbName);
        name = db.connectionName();
        ok = db.open();
        if(!ok)
        {
            qInfo() << "open database failed, " << db.lastError().text();
        }

        if(!db.isOpen())
            return;
        QSqlQuery query(db);
        query.exec(sql);
        record = query.record();
        while (query.next())
        {
            for(int i = 0; i < record.count(); i++)
            {
                data.append(query.value(i));
            }
        }
        db.close();
    }
    catch(const QSqlError &e)
    {
        qInfo() << e.text();
    }

    QSqlDatabase::removeDatabase(name);
    return;
}

